import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
@Injectable({
  providedIn: 'root'
})
export class DeviceService {
  private device = '/Equipment';
  constructor(private http:HttpClient) { }
  private createPaginationParams(currentPage: number, itemsPerPage: number): HttpParams {
    return new HttpParams()
      .set('page', currentPage.toString())
      .set('pageSize', itemsPerPage.toString());
  }// 创建分页参数
  GetPagedEquipmentCategoriesWithChildren(page?: number, pageSize?: number){
    let params = new HttpParams();
    if (page !== undefined && pageSize !== undefined) {
      params = this.createPaginationParams(page, pageSize);
    }
    return this.http.get(environment.apiUrl+ this.device+'/GetPagedEquipmentCategoriesWithChildren',{
      params: params
    })
  }// 获取所有设备分类
  CreateEquiomentCategory(data:any){
    const url = this.device+'/CreateEquiomentCategory';
    return this.http.post(environment.apiUrl+ url,data);
  }// 创建设备分类
  DeleteEquipmentCategory(id:number){
    const url = this.device+'/DeleteEquipmentCategory';
    return this.http.delete(environment.apiUrl+ url,{params:{id}});
  }// 删除设备分类
  UpdateEquipmentCategory(data:any, id:number){
    const url = this.device+'/UpdateEquipmentCategory';
    return this.http.put(environment.apiUrl+ url,data,{params:{id}});
  }// 修改设备分类
  GetEquipments(EquipmentCategoryId?:number,Status?:string,Model?:string,Site?:string,SearchTerm?:string,PageNumber?:number,PageSize?:number){
    let params = new HttpParams();
    if (EquipmentCategoryId ) {
      params = params.set('EquipmentCategoryId', EquipmentCategoryId.toString());
    }
    if (Status ) {
      params = params.set('Status', Status);
    }
    if (Model ) {
      params = params.set('Model', Model);
    }
    if (Site ) {
      params = params.set('Site', Site);
    }
    if (SearchTerm ) {
      params = params.set('SearchTerm', SearchTerm);
    }
    if (PageNumber) {
      params = params.set('PageNumber', PageNumber.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(environment.apiUrl+ this.device+'/GetEquipments',{
      params: params
    })
  }// 获取所有设备
  CreateEquioment(data:any){
    const url = this.device+'/CreateEquioment';
    return this.http.post(environment.apiUrl+ url,data);
  }// 创建设备
  GetEquipmentById(id:number){
    const url = this.device+'/GetEquipmentById';
    return this.http.get(environment.apiUrl+ url,{params:{id}});
  }// 获取设备详情
  UpdateEquipment(data:any, id:number){
    const url = this.device+'/UpdateEquipment';
    return this.http.put(environment.apiUrl+ url,data,{params:{id}});
  }// 修改设备
  downloadEquipmentCategoryFileModel(){
    const url = '/Excel/DownloadEquipmentCategoryFileModel';
    return this.http.get(environment.apiUrl+ url,{responseType: 'blob'});
  }// 下载设备分类模板
  DeleteEquipment(id:number){
    const url = this.device+'/DeleteEquipment';
    return this.http.delete(environment.apiUrl+ url,{params:{id}});
  }// 删除设备
  DeleteEquipments(ids:number[]){
    const url = this.device+'/DeleteEquipments';
    const options = {
      body: ids
    };
    return this.http.delete(environment.apiUrl+ url,options);
  }// 批量删除设备

}
